package cn.skyclass.net
{
	import cn.skyclass.controls.AdvancedAlert;
	import cn.skyclass.utils.Log4Flex;
	
	import flash.external.ExternalInterface;
	
	import mx.core.Application;
	import mx.utils.URLUtil;
	
	/**
	 * 和当前网页的 url 相关的一些处理
	 * 
	 * 使用此类的函数，需要在网页内包含下面的 javascript 函数
	 * function getCurrentUrl(){
	 * 		return window.location.href;
	 * }
	 * function getClientIp(){
	 * 		return "192.168.0.1"; // should be generated by jsp
	 * }
	 */
	public class URLUtilEx
	{
		/**
		 * 获取当前 swf 文件所在的网页的 url 全路径
		 * eg: http://localhost:8080/web-tester/page/default.jsp?id=1
		 */
		public static function getCurrentUrl():String{			
			var value:String = ExternalInterface.call("getCurrentUrl"); 
			if(value == null){
				value = "";
				
				var msg:String = "call javascript function getCurrentUrl failed in this page.";
				msg += "\nfunction content:";
				msg += "\nfunction getCurrentUrl(){ return window.location.href; }";
				
				AdvancedAlert.info(msg);
				new Log4Flex().info(msg);
			}
			return value;	
		}
		
		/**
		 * get the client machine ip
		 * eg: 192.168.1.1
		 */
		public static function getClientIp():String{
			var value:String = ExternalInterface.call("getClientIp"); 
			return (value == null) ? "" : value;
		}	
		
		/**
		 * swf 应用程序的 url
		 * eg: http://localhost:8080/web-tester/page/index.swf
		 */
		public static function getAppUrl():String{
			var app:Application = Application.application as Application;
			return app ? app.url : "";
		}
		
		/**
		 * 获取当前网页所在站点主机路径部分
		 * eg: http://localhost:8080
		 */
		public static function getRootUrl():String{		
			var url:String = getAppUrl();	
			return URLUtil.getProtocol(url) + "://" + URLUtil.getServerNameWithPort(url);
		}
		
		/**
		 * 获取当前网页所在站点的上下文路径
		 * eg: /web-tester
		 */
		public static function getContextPath():String{		
			var url:String = getAppUrl();	
			var root:String = URLUtil.getProtocol(url) + "://" + URLUtil.getServerNameWithPort(url);
			var context:String = url.substr(root.length + 1);
			context = context.substring(0, context.indexOf("/"));
			return "/" + context;
		}
		
		/**
		 * 获取当前网页所在站点的根目录
		 * http://localhost:8080/web-tester/
		 */
		public static function getSiteRootUrl():String{
			var out:String = "";
			out += URLUtilEx.getRootUrl();
			out += URLUtilEx.getContextPath() + "/";
			return out;
		}	
				
		/**
		 * 获取当前 url 所带的全部参数，直接截取 ? 后面的字符串
		 * eg: a=1&b=2&c=leo
		 */
		public static function getQueryParams():String{
			var url:String = getCurrentUrl();
			var i:int = url.indexOf("?");
			if(i >= 0){
				return url.substring(i + 1);
			}else{
				return "";
			}			
		}
		
		/**
		 * 当前 url 是否有参数
		 */
		public static function hasQueryParams():Boolean{
			var params:String = getQueryParams();
			if(params){
				var ieq:int = params.indexOf("=");
				return (ieq >= 0);
			}else{
				return false;
			}
		}
						
						
		/**
		 * 根据当前网页的路径，将相对组装成一个完整路径。通过 URLUtil.getFullURL(rootUrl, url) 函数实现。 
		 * 
		 * eg: 假如当前路径为 http://localhost:8080/web-tester/page/default.jsp ，将有下列结果
		 * eg1: /web-tester/page/default1.jsp --> http://localhost:8080/web-tester/page/default1.jsp
		 * eg2: /page/default1.jsp --> http://localhost:8080/page/default1.jsp
		 * eg3: default2.jsp --> http://localhost:8080/web-tester/page/default2.jsp
		 */
		public static function getFullUrl(relativeUrl:String, rootUrl:String=null):String{	
			if(rootUrl){
				return URLUtil.getFullURL(rootUrl, relativeUrl);
			}else{
				return URLUtil.getFullURL(getAppUrl(), relativeUrl);
			}
		}	
		
	}
}